/*
 * InstallCellRenderer.java
 *
 * Created on August 4, 2008, 3:48 PM
 */

package jwu2.gui.table;

import java.awt.Component;
import java.util.EventObject;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.event.CellEditorListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import jwu2.core.Addon;
import jwu2.gui.AddonInfoFrame;
import jwu2.gui.GUIEventDispatcher;

/**
 *
 * @author  jbk
 */
public class InstallCellEditor extends javax.swing.JPanel implements TableCellRenderer, TableCellEditor {
    private Addon thisAddon = null;
    private AddonTable.tableType tableType;

    /** Creates new form InstallCellRenderer */
    public InstallCellEditor(AddonTable.tableType tableType) {
        initComponents();
        this.tableType = tableType;
        if (tableType == AddonTable.tableType.REMOVE || tableType == AddonTable.tableType.REMOVE_BUNDLE){
            installLabel.setText("<html><body><a href='remove'>Remove</a></body></html>");
        } else if (tableType == AddonTable.tableType.ADD_BUNDLE){
            installLabel.setText("<html><body><a href='remove'>Bundle</a></body></html>");
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        name = new javax.swing.JLabel();
        moreLabel = new javax.swing.JLabel();
        installLabel = new javax.swing.JLabel();

        name.setText("jLabel1");
        name.setMinimumSize(new java.awt.Dimension(4, 15));

        moreLabel.setText("<html><body><a href=\"more\">More</a></body></html>");
        moreLabel.setName("more"); // NOI18N
        moreLabel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                moreLabelMouseClicked(evt);
            }
        });

        installLabel.setText("<html><body><a href=\"more\">Install</a></body></html>");
        installLabel.setName("install"); // NOI18N
        installLabel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                installLabelMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(installLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 61, Short.MAX_VALUE)
            .addComponent(moreLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 61, Short.MAX_VALUE)
            .addComponent(name, javax.swing.GroupLayout.DEFAULT_SIZE, 61, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(name, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, 0)
                .addComponent(moreLabel)
                .addGap(0, 0, 0)
                .addComponent(installLabel))
        );
    }// </editor-fold>//GEN-END:initComponents

private void moreLabelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_moreLabelMouseClicked
    if (thisAddon!=null){
        new AddonInfoFrame(thisAddon).setVisible(true);
    }
}//GEN-LAST:event_moreLabelMouseClicked

private void installLabelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_installLabelMouseClicked
    if (thisAddon!=null){
        if (tableType==AddonTable.tableType.ADD){
            GUIEventDispatcher.fireInstallAddonEvent(thisAddon);
        } else if (tableType==AddonTable.tableType.REMOVE){
            GUIEventDispatcher.fireRemoveAddonEvent(thisAddon);
        } else if (tableType==AddonTable.tableType.ADD_BUNDLE){
            GUIEventDispatcher.fireBundleAddAddonEvent(thisAddon);
        } else if (tableType==AddonTable.tableType.REMOVE_BUNDLE){
            GUIEventDispatcher.fireBundleRemoveAddonEvent(thisAddon);
        } 
    }    
}//GEN-LAST:event_installLabelMouseClicked


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel installLabel;
    private javax.swing.JLabel moreLabel;
    private javax.swing.JLabel name;
    // End of variables declaration//GEN-END:variables

    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        String text = "";
        if (value!=null){
            thisAddon = (Addon) value;
            text = thisAddon.getName();
        } else {
            thisAddon = null;
        }
         
        if (isSelected) {
            moreLabel.setVisible(true);
            installLabel.setVisible(true);
            this.setBackground(table.getSelectionBackground());
        } else {
            moreLabel.setVisible(false);
            installLabel.setVisible(false);
            this.setBackground(table.getBackground());
        }
        name.setText(text);
        
        return this;
    }

    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
        return getTableCellRendererComponent(table, value, isSelected, isSelected, row, column);
    }

    public Object getCellEditorValue() {
        return thisAddon;
    }

    public boolean isCellEditable(EventObject anEvent) {
        return true;
    }

    public boolean shouldSelectCell(EventObject anEvent) {
        return true;
    }

    public boolean stopCellEditing() {
        return true;
    }

    public void cancelCellEditing() {
        //
    }

    public void addCellEditorListener(CellEditorListener l) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public void removeCellEditorListener(CellEditorListener l) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

}
